var pagesize = 15;
var pageNow = 1;
var pagecount = 0;
var count = 0;
var category = '';
var menu_index=0;

$(function(){

	////
	option_menu(); //菜单函数

	if (typeof location.search) {
		var value = location.search;

		if (value) {
			var array = value.split('=');
			if(array.length!=3){
				alert('参数传递错误！');
				return false;
			}else{
				menu_index = array[1].split('&')[0];

				show_menu(menu_index); //显示菜单项
				category = decodeURI(array[2]); //获取消息类型
				$('#c_n').html(category);
			}
		}
	}

	ajax('InformationAction', 'pageNow=1&category=' + category);  //异步获取数据,生成表格
	//搜索函数
	$("#a_search")
	.click(
			function() {

				var value = $("#searchtext").val();
				if (Trim(value) != "") {
					ajax('InformationAction', 'pageNow='
							+ pageNow + "&target=" + value
							+ "&category=" + category);
				} else {
					alert("没有输入关键字!");
				}
				//将选择框复原
				$("#dselect").children(":first").attr("selected",
				"true");
			});
	//下拉框变动
	$("#dselect")
	.change(
			function() {

				var department = $(".select_:selected").val();
				if (department == "#") {
					department = "全部";
				}
				ajax('InformationAction', 'pageNow=' + pageNow
						+ "&target=" + department + "&category="
						+ category);
				$("#searchtext").val("");
			});

});



//首页
function firstpage()
{
	if(pageNow!=1)
	{
		pageNow=1;
		var value=$("#searchtext").val();
		var department=$(".select_:selected").val();

		var mm='';
		if(Trim(value)!=""){
			mm+=Trim(value);
			if(department!="#"){
				mm+=';'+department;
			}
		}else{
			if(department!="#"){
				mm=department;
			}
		}

		ajax('InformationAction','pageNow='+pageNow+"&target="+mm+'&category=' + category);

	}

}
//最后一页
function lastpage()
{
	if(pageNow!=pagecount)
	{
		pageNow=pagecount;
		var value=$("#searchtext").val();
		var department=$(".select_:selected").val();

		var mm='';
		if(Trim(value)!=""){
			mm+=Trim(value);
			if(department!="#"){
				mm+=';'+department;
			}
		}else{
			if(department!="#"){
				mm=department;
			}
		}

		ajax('InformationAction','pageNow='+pageNow+"&target="+mm+'&category=' + category);


	}
}
//前一页
function prepage()
{
	if(pageNow!=1)
	{
		pageNow=pageNow-1;
		var value=$("#searchtext").val();
		var department=$(".select_:selected").val();

		var mm='';
		if(Trim(value)!=""){
			mm+=Trim(value);
			if(department!="#"){
				mm+=';'+department;
			}
		}else{
			if(department!="#"){
				mm=department;
			}
		}

		ajax('InformationAction','pageNow='+pageNow+"&target="+mm+'&category=' + category);

	}
}
//后一页
function nextpage()
{

	if(pageNow!=pagecount)
	{
		pageNow=pageNow+1;

		var value=$("#searchtext").val();
		var department=$(".select_:selected").val();

		var mm='';
		if(Trim(value)!=""){
			mm+=Trim(value);
			if(department!="#"){
				mm+=';'+department;
			}
		}else{
			if(department!="#"){
				mm=department;
			}
		}

		ajax('InformationAction','pageNow='+pageNow+"&target="+mm+'&category=' + category);

	}

}
//跳到多少页
function thepage(e)
{
	if(pageNow!=e)
	{
		pageNow=e;

		var value=$("#searchtext").val();
		var department=$(".select_:selected").val();

		var mm='';
		if(Trim(value)!=""){
			mm+=Trim(value);
			if(department!="#"){
				mm+=';'+department;
			}
		}else{
			if(department!="#"){
				mm=department;
			}
		}
		ajax('InformationAction','pageNow='+pageNow+"&target="+mm+'&category=' + category);
	}
}
//跳转到多少页
function turntopage()
{
	var page=$(".turnpage").val();
	if(page.search(/^[1-9]{1,250}$/)==-1||parseInt(page)>pagecount){
		alert('请输入合法数字！');
		return;
	}


	if(page!=pageNow&&page<=pagecount&&page>=1)
	{
		pageNow=page;
		var value=$("#searchtext").val();
		var department=$(".select_:selected").val();

		var mm='';
		if(Trim(value)!=""){
			mm+=Trim(value);
			if(department!="#"){
				mm+=';'+department;
			}
		}else{
			if(department!="#"){
				mm=department;
			}
		}

		ajax('InformationAction','pageNow='+pageNow+"&target="+mm+'&category=' + category);


	}
}


//异步获取数据，生成表格函数
function ajax(url,param){

	ajax_post(url,param,function(data){
		var table=$("#show");
		table.empty();
		var html='';
		var count=0;
		if(typeof data.pageCount){
			count=data.pageCount;
		};
		var len=data.list.length;//返回数据长度
		//alert(len);
		if(len!=0)
		{
			html="<tr bgcolor=''>" +
			"<td height='24' colspan='10' style='font-weight: bolder;'>消息列表:&nbsp;</td>" +
			"</tr>" +
			"<tr align='center' bgcolor='' height='22'>" +
			"<th width=''>标题</th>" +
			"<th width=''>关键字</th>" +
			"<th width='15%'>附件</th>" +
			"<th width='20%'>发布时间</th>" +
			"</tr>";
			for(var i=0;i<len;i++)
			{
				var message=data.list[i];
				var id=message.id;
				var title=message.title;
				if(title.length>20){

					title=title.substring(0,20)+'...';
				}
				var keywords=message.keywords;
				if(keywords.length>15){

					keywords=keywords.substring(0,15)+'...';
				}
				var file=message.file;
				var time=formatTime(new Date(message.time));

				html+="<tr align='center' bgcolor='' onMouseMove=this.bgColor='#FFFFFF' onMouseOut=this.bgColor='' height='22' >" +
				"<td align='left'><a href='index-preview.html?menu_index="+menu_index+"&category="+category+"&id="+id+"' target='_blank'>"+title+"</a></td>" +
				"<td>"+keywords+"</td>" +
				"<td>"+file+"</td>" +
				"<td>"+time+"</td>" ;
			}

			html+=paging(pageNow,pagesize,count);
			table.append(html);
		}else{
			$('#table_1').empty();
			alert('抱歉，没有查找到任何记录!');
		}

	});
}

function paging(pageNow,pagesize,count){

	if(count%pagesize==0)
	{
		pagecount=count/pagesize;
	}else{
		pagecount=parseInt(count/pagesize+1);
	}
	var page="<tr align='right'><td height='30' colspan='10' align='center'><ul id='lie_'><li>共<span>"+count+"</span>条记录 </li><li class='s_w'><a href='javascript:' onclick='firstpage();'>首页</a></li><li class='s_x'><a href='javascript:' onclick='prepage();'>上一页</a></li>";
	if(pagecount<=10)
	{
		for(var k=1;k<=pagecount;k++)
		{
			if(k==pageNow)
			{
				page+=" <li><a href='javascript:;' style='color: red;border:none;text-decoration: none;background-color:white;font-weight:bolder;'>"+k+"</a></li>";
			}
			else{
				page+=" <li><a href='javascript:;' onclick='thepage("+k+");'>"+k+"</a></li>";
			}
		}
	}
	else{
		if(pageNow<=5)
		{
			for(var k=1;k<=10;k++)
			{
				if(k==pageNow)
				{
					page+=" <li><a href='javascript:;' style='color: red;border:none;text-decoration: none;background-color:white;font-weight:bolder;'>"+k+"</a></li>";
				}
				else{
					page+=" <li><a href='javascript:;' onclick='thepage("+k+");'>"+k+"</a></li>";
				}
			}
		}
		else{
			var startpage=parseInt(pageNow)-5;
			var endpage=parseInt(pageNow)+4;
			//alert(endpage);
			var  epage;
			if(pagecount<endpage)
			{
				epage=pagecount;
			}
			else{
				epage=endpage;
			}

			for(var k=startpage;k<=epage;k++)
			{
				if(k==pageNow)
				{
					page+=" <li><a href='javascript:;' style='color: red;border:none;text-decoration: none;background-color:white;font-weight:bolder;'>"+k+"</a></li>";
				}
				else{
					page+=" <li><a href='javascript:;' onclick='thepage("+k+");'>"+k+"</a></li>";
				}
			}
		}
	}
	page+=" <li class='s_x'><a href='javascript:;' onclick='nextpage();'>下一页</a></li>	<li class='s_w'><a href='javascript:' onclick='lastpage();'>尾页</a></li>	<li> 跳转到第: </li> <li><input type='text' class='turnpage' style='width:40px;font-weight:bolder;'/></li> <li> 页 </li><li><input type='button' id='m_search' onclick='turntopage();'/> </li> </ul></td></tr>";
	return page;
}
///去掉字符串前尾的空格
function Trim(str) {
	return str.replace(/(\s*)|(\s*$)/g, "");
}

function ajax_post(the_url,the_param,succ_callback){
	$.ajax({
		url:the_url,
		type:"post",
		data:the_param,
		datatype:"json",
		success:succ_callback,
		error:function(html){
			alert("提交数据失败，代码："+html.status+"请稍后");
		}
	});
}